package me.spring.cloud.components.starter.elastic.job.lite.configure;

import com.dangdang.ddframe.job.event.JobEventConfiguration;
import com.dangdang.ddframe.job.event.rdb.JobEventRdbConfiguration;
import me.spring.cloud.common.components.elasticjob.lite.base.ElasticJobAttributeTag;
import javax.sql.DataSource;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;


/**
 * 任务自动配置
 */
@Configuration
public class ElasticJobDbLogConfiguration {

  @ConditionalOnProperty(prefix = ElasticJobAttributeTag.ELASTIC_JOB_LOG_PREFIX, name = "enable", havingValue = "true")
  @Bean(ElasticJobAttributeTag.ELASTIC_JOB_LOG_DS_NAME)
  @ConfigurationProperties(ElasticJobAttributeTag.ELASTIC_JOB_LOG_DS_PREFIX)
  public DataSource createElasticJobLogDatasource() {
    return DataSourceBuilder.create().build();
  }

  @Bean
  public JobEventConfiguration jobEventConfiguration() {
    return new JobEventRdbConfiguration(createElasticJobLogDatasource());
  }

}
